package com.unnamed.collisions.geometry;
import java.awt.Graphics;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.swing.JPanel;

import com.aponisipsis.gl3d.geometry.Face;
import com.aponisipsis.gl3d.geometry.Mesh2D;
import com.aponisipsis.gl3d.importer.json.JSONLoader;
import com.aponisipsis.gl3d.importer.json.JSONMeshData;
import com.aponisipsis.gl3d.importer.json.JSONSceneData;


public class HullTest extends JPanel{
	Polygon p;
	
	public HullTest(){
		super();
		
		
	}
	
	public void paint(Graphics g){
		
	}
	
	public static Polygon convertJSONMesh(JSONMeshData mesh) throws Exception{
		SharedPoint[] points = new SharedPoint[mesh.vertices.length];
		
		for(int c=0;c<mesh.vertices.length;c++){
			points[c]=new SharedPoint(mesh.vertices[c][0], mesh.vertices[c][1]);
			points[c].neighbours = new ArrayList<SharedPoint>();
			points[c].name = ""+c;
		}
		
		for(int c=0;c<mesh.faces.length;c++){
			for(int d=0;d<mesh.faces[c].length;d++){
				if(mesh.faces[c].length!=3){
					throw new Exception("Only 3 vertex per face allowed");
				}
				SharedPoint point = points[mesh.faces[c][d]];
				
				for(int e=0;e<mesh.faces[c].length;e++){
					if(point!=points[mesh.faces[c][e]]){
						System.out.println("Point "+point+" -> Neighbor "+points[mesh.faces[c][e]]);
						if(!point.neighbours.contains(points[mesh.faces[c][e]])){
							point.neighbours.add(points[mesh.faces[c][e]]);
						}
					}
				}
			}
		}
		
		List<SharedPoint> hull = SharedPoint.getHull(points[0]);
		
		Point[] poSharedPoints = new Point[hull.size()];
		
		for(int c=0;c<hull.size();c++){
			poSharedPoints[c]=new Point(hull.get(c).x, hull.get(c).y);
			System.out.println("Point "+poSharedPoints[c]);
			//for(int d=0;d<poSharedPoints[c].)
		}
		
		return new Polygon(poSharedPoints);
	}
	public static List<Polygon> getHull() throws Exception {
		JSONSceneData scene = new JSONLoader().load(new FileInputStream(new File("d:/3d/testscene.json")), JSONSceneData.class);
		List<Polygon> p = new ArrayList<Polygon>();
		for(JSONMeshData m:scene.meshes){
			p.add(convertJSONMesh(m));
		}
		
		return p;
		
		
	}
	
	public static Polygon getTestHull(){
		SharedPoint[] point = new SharedPoint[12];
		
		point[0] = new SharedPoint("0",10,10);
		point[1] = new SharedPoint("1",10,20);
		point[2] = new SharedPoint("2",10,40);
		point[3] = new SharedPoint("3",10,50);
		point[4] = new SharedPoint("4",20,50);
		point[5] = new SharedPoint("5",50,50);
		point[6] = new SharedPoint("6",50,40);
		point[7] = new SharedPoint("7",20,40);
		point[8] = new SharedPoint("8",20,20);
		point[9] = new SharedPoint("9",50,20);
		point[10] = new SharedPoint("10",50,10);
		point[11] = new SharedPoint("11",20,10);
		
		point[0].neighbours = Arrays.asList(new SharedPoint[]{point[1],point[8],point[11]});
		point[1].neighbours = Arrays.asList(new SharedPoint[]{point[0],point[8],point[7],point[2]});
		point[2].neighbours = Arrays.asList(new SharedPoint[]{point[1],point[7],point[4],point[3]});
		point[3].neighbours = Arrays.asList(new SharedPoint[]{point[2],point[4]});
		point[4].neighbours = Arrays.asList(new SharedPoint[]{point[2],point[7],point[5],point[3]});
		point[5].neighbours = Arrays.asList(new SharedPoint[]{point[4],point[7],point[6]});
		point[6].neighbours = Arrays.asList(new SharedPoint[]{point[5],point[7]});
		point[7].neighbours = Arrays.asList(new SharedPoint[]{point[1],point[2],point[4],point[5],point[6],point[8]});
		point[8].neighbours = Arrays.asList(new SharedPoint[]{point[7],point[1],point[0],point[11],point[9]});
		point[9].neighbours = Arrays.asList(new SharedPoint[]{point[8],point[11],point[10]});
		point[10].neighbours = Arrays.asList(new SharedPoint[]{point[9],point[11]});
		point[11].neighbours = Arrays.asList(new SharedPoint[]{point[10],point[9],point[8],point[0]});
	
		List<SharedPoint> hull = SharedPoint.getHull(point[0]);
		
		Point[] points = new Point[hull.size()];
		for(int c=0;c<point.length;c++){
			points[c]=new Point(hull.get(c).x,hull.get(c).y);
		}
		return new Polygon(points);
	}
}
